//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package ming.util.base;

import com.alibaba.fastjson.JSON;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

public class PlUploadServlet extends HttpServlet {
    public PlUploadServlet() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fileFolder = request.getParameter("fileFolder");
        if (StringUtils.isBlank(fileFolder)) {
            fileFolder = "/temp";
        }

        String datefolder = "/" + DateUtil.dateToString(new Date(), "yyyy");
        datefolder = "";
        String webParentPath = (new File(request.getSession().getServletContext().getRealPath("/"))).getParent();
        String realPath = webParentPath + ConfigUtil.get("uploadPath") + fileFolder + datefolder;
        System.out.println("realPath:" + realPath);
        String path = ConfigUtil.get("uploadPath") + fileFolder + datefolder;
        System.out.println("path:" + path + "||" + request.getSession() + "||" + request.getSession().getAttribute(ConfigUtil.getSessionInfoName()) + "||" + ConfigUtil.getSessionInfoName());
        if (request.getSession() == null) {
            Map<String, Object> m = new HashMap();
            m.put("status", false);
            response.getWriter().write(JSON.toJSONString(m));
        } else {
            File up = new File(realPath);
            if (!up.exists()) {
                up.mkdirs();
            }

            response.setCharacterEncoding("UTF-8");
            Integer chunk = null;
            Integer chunks = null;
            String tempFileName = null;
            String newFileName = null;
            BufferedOutputStream outputStream = null;
            if (ServletFileUpload.isMultipartContent(request)) {
                try {
                    Map<String, Object> m = new HashMap();
                    try {
                        DiskFileItemFactory factory = new DiskFileItemFactory();
                        factory.setSizeThreshold(1024);
                        ServletFileUpload upload = new ServletFileUpload(factory);
                        upload.setHeaderEncoding("UTF-8");
                        List<FileItem> items = upload.parseRequest(request);
                        Iterator var18 = items.iterator();

                        while(var18.hasNext()) {
                            FileItem item = (FileItem)var18.next();
                            if (item.isFormField()) {
                                if (item.getFieldName().equals("name")) {
                                    tempFileName = item.getString();
                                    System.out.println("临时文件名：" + tempFileName);
                                } else if (item.getFieldName().equals("chunk")) {
                                    chunk = Integer.parseInt(item.getString());
                                    System.out.println("当前文件块：" + (chunk + 1));
                                } else if (item.getFieldName().equals("chunks")) {
                                    chunks = Integer.parseInt(item.getString());
                                    System.out.println("文件总分块：" + chunks);
                                }
                            } else if (tempFileName != null) {
                                String name = item.getName();
                                System.out.println(name);
                                File savedFile;
                                if (name.endsWith(".jpg")) {
                                    savedFile = new File(realPath, name);
                                    item.write(savedFile);
                                }

                                String chunkName = tempFileName;
                                if (chunk != null) {
                                    chunkName = chunk + "_" + tempFileName;
                                }

                                savedFile = new File(realPath, chunkName);
                                item.write(savedFile);
                            }
                        }

                        newFileName = UUID.randomUUID().toString().replace("-", "").concat(".").concat(FilenameUtils.getExtension(tempFileName));
                        if (chunks == null) {
                            newFileName = tempFileName;
                        }

                        if (chunk != null && chunk + 1 == chunks) {
                            outputStream = new BufferedOutputStream(new FileOutputStream(new File(realPath, newFileName)));

                            for(int i = 0; i < chunks; ++i) {
                                File tempFile = new File(realPath, i + "_" + tempFileName);
                                byte[] bytes = FileUtils.readFileToByteArray(tempFile);
                                outputStream.write(bytes);
                                outputStream.flush();
                                tempFile.delete();
                            }

                            outputStream.flush();
                        }

                        //
                        m.put("status", true);
                        m.put("fileUrl", realPath + "/" + newFileName);
                        response.getWriter().write(JSON.toJSONString(m));
                    } catch (FileUploadException var32) {
                        var32.printStackTrace();
                        m = new HashMap();
                        m.put("status", false);
                        response.getWriter().write(JSON.toJSONString(m));
                    } catch (Exception var33) {
                        var33.printStackTrace();
                        m = new HashMap();
                        m.put("status", false);
                        response.getWriter().write(JSON.toJSONString(m));
                    }
                } finally {
                    try {
                        if (outputStream != null) {
                            outputStream.close();
                        }
                    } catch (IOException var31) {
                        var31.printStackTrace();
                    }

                }
            }
        }

    }
}
